<?php
/**
 * Created by PhpStorm.
 * User: 沁塵
 * Date: 2019/4/20
 * Time: 19:57
 */

namespace app\api\controller\v1;

use app\api\model\Product as ProductModel;
use think\Request;

class Product
{

    /**
     * 查询所有商品
     * @return mixed
     */
    public function getProduct(Request $request)
    {
        $params = $request->get();
        $pageSize = $params['pageSize'] ?? 50;
        $category = empty($params['title']) ? '' : $params['title'];
        $list = ProductModel::where('title','like','%'.$category.'%')
            ->where("platform", $params["platform"])
            ->paginate($pageSize);
        return writeJson(200,$list,'商品列表！');
    }

    public function addProduct(Request $request)
    {
        $params = $request->post();
        $data = [
            'title' => $params['title'],
            'category' => $params['category'],
            'topic' => $params['topic'],
            'product_id' => $params['product_id'],
            'product_name' => $params['product_name'],
            'script' => $params['script'],
            'platform' => $params['platform'],
            'create_time' => date("Y-m-d H:i:s")
        ];
        $res = ProductModel::create($data);
        return writeJson(200,$res,'新增成功！');
    }

    public function updateProduct(Request $request)
    {
        $params = $request->post();
        $update = [
            'title' => $params['title'],
            'category' => $params['category'],
            'topic' => $params['topic'],
            'product_id' => $params['product_id'],
            'product_name' => $params['product_name'],
            'script' => $params['script'],
            'platform' => $params['platform'],
            'create_time' => date("Y-m-d H:i:s")
        ];
        $res = ProductModel::where('id',$params['id'])->update($update);
        return writeJson(200,$res,'更新成功！');
    }

}